package com.fjb.controller.order;


import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fjb.common.entity.HttpCode;
import com.fjb.common.entity.JsonPagesResult;
import com.fjb.common.entity.JsonResult;
import com.fjb.common.entity.JsonTotalResult;
import com.fjb.common.entity.PageQuery;
import com.fjb.common.enums.user.AddressStatus;
import com.fjb.controller.base.BaseController;
import com.fjb.dto.AddOrderCart;
import com.fjb.dto.LoginUserInfo;
import com.fjb.pojo.cart.ShoppingCart;
import com.fjb.pojo.order.OrderInfo;
import com.fjb.pojo.order.query.OrderInfoQuery;
import com.fjb.pojo.order.vo.OrderInfoPageVo;
import com.fjb.pojo.product.query.ProductInfoQuery;
import com.fjb.pojo.product.vo.ProductInfoPageVo;
import com.fjb.pojo.user.SysUserAddress;
import com.fjb.service.order.OrderInfoService;
import com.fjb.service.user.SysUserAddressService;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

/**
 * <p>
 * 订单信息 前端控制器
 * </p>
 *
 * @author hemiao
 * @since 2019-12-22
 */
@Controller
@RequestMapping("/orderInfo")
public class OrderInfoController extends BaseController{
	
	@Autowired
	private OrderInfoService orderInfoService;
	@Autowired
	private SysUserAddressService sysUserAddressService;
	
	/**
	 * @Description:立即下单
	 * @param request
	 * @param addressId
	 * @return
	 * JsonResult<Integer>
	 * @exception:
	 * @author: hemiao
	 * @time:2020年2月10日 下午11:43:29
	 */
	@RequestMapping(value="/addOreder")
	@ResponseBody
	public JsonResult<Integer> addOreder(HttpServletRequest request,Integer addressId){
		HttpCode httpCode = HttpCode.PARAM_VERIFICATION;
		if(addressId==null) {
			httpCode.setMsg("收货地址不能为空");
			return new JsonResult<Integer>(null, httpCode);
		}
		
		String cartListStr = request.getParameter("cartListStr");
		if(StringUtils.isBlank(cartListStr)) {
			return new JsonResult<Integer>(null, httpCode);
		}
		List<AddOrderCart> addOrderCartList = JSON.parseArray(cartListStr, AddOrderCart.class);
		if(addOrderCartList.size()==0) {
			httpCode.setMsg("生成订单参数解析失败");
			return new JsonResult<Integer>(null, httpCode);
		}
		LoginUserInfo userInfo = getBaseWxTokenId(request);
		Integer userId = userInfo.getUserId();
		
		QueryWrapper<SysUserAddress> queryWrapper = new QueryWrapper<SysUserAddress>();
		queryWrapper.eq("id", addressId);
		queryWrapper.eq("user_id", userId);
		queryWrapper.eq("address_status", AddressStatus.STATUS_1.getCode());
		SysUserAddress address = sysUserAddressService.getById(addressId);
		if(address==null) {
			httpCode.setMsg("收货地址没找到");
			return new JsonResult<Integer>(null, httpCode);
		}
		return orderInfoService.addOreder(userInfo, addOrderCartList, address);
	}
	
	/**
	 * @Description:取消下单
	 * @param request
	 * @return
	 * JsonResult<List<ShoppingCart>>
	 * @exception:
	 * @author: hemiao
	 * @time:2020年2月11日 下午9:36:02
	 */
	public JsonResult<List<ShoppingCart>> updateCancelOreder(HttpServletRequest request){
		
		// 库存增加
		
		return new JsonResult<List<ShoppingCart>>(null, HttpCode.PARAM_VERIFICATION);
	}
	
	/**
	 * @Description:分页列表查询
	 * @param request
	 * @param pageQuery
	 * @return
	 * JsonPagesResult<List<OrderInfoPageVo>>
	 * @exception:
	 * @author: hemiao
	 * @time:2020年2月11日 上午12:27:34
	 */
	@RequestMapping(value = "/selectPageList")
    @ResponseBody
    public JsonPagesResult<List<OrderInfoPageVo>> selectPageList(HttpServletRequest request,
    		PageQuery pageQuery,Integer orderStatus){
		OrderInfoQuery orderQuery = new OrderInfoQuery();
		LoginUserInfo userInfo = getBaseWxTokenId(request);
		Integer userId = userInfo.getUserId();
		orderQuery.setUserId(userId);
		if(orderStatus!=null) {
			orderQuery.setOrderStatus(orderStatus);
		}
        return orderInfoService.selectPageList(pageQuery,orderQuery);
    }
}
